<template>
  <div class="app-container">
    <div class="select-box">
      <el-select
        v-model="years"
        placeholder="请选择"
        size="small"
        style="width: 120px; margin-right: 10px"
        @change="change"
      >
        <el-option
          v-for="item in year"
          :key="item"
          :label="item"
          :value="item"
        />
      </el-select>
      <el-select
        v-model="month"
        placeholder="请选择"
        size="small"
        style="width: 120px"
        @change="change"
      >
        <el-option
          v-for="item in 12"
          :key="item"
          :label="item"
          :value="item"
        />
      </el-select>
    </div>
    <!-- 显示日历图表 -->
    <el-calendar v-model="currentDate">
      <template #dateCell="{data,date}">
        <div class="date-content">
          <span class="text" :class="{current: data.isSelected}">
            {{ data.day.slice(-2) }}
          </span>
          <span v-if="isWeek(date)" class="rest">休</span>
        </div>
      </template>
    </el-calendar>
  </div>
</template>

<script>
export default {
  data() {
    return {
      years: '',
      month: '',
      currentDate: new Date()
    }
  },
  computed: {
    year() {
      /*  const arr = []
      for (let index = this.years - 10; index <= this.years + 10; index++) {
        arr.push(index)
      }
      return arr */
      return Array.from(new Array(21), (item, index) => index + this.years - 10)
    }
  },
  // 3.图表数据发生该变时 下拉框的值也要改变
  watch: {
    currentDate(e) {
      // console.log(e) // Sat Oct 23 2021 08:00:00 GMT+0800 (中国标准时间)
      this.years = new Date(e).getFullYear()
      this.month = new Date(e).getMonth() + 1
    }
  },
  created() {
    const d = new Date()
    this.years = d.getFullYear()
    this.month = d.getMonth() + 1
  },
  methods: {
    // 2.实现 下拉框改变月份数据图表跟着改变
    change() {
      this.currentDate = new Date(this.years, this.month - 1)
    },
    isWeek(date) {
      return date.getDay() === 6 || date.getDay() === 0
    }
  }
}
</script>

<style lang="scss" scoped>
.select-box {
  display: flex;
  justify-content: flex-end;
}
::v-deep {
  .el-calendar__header {
    display: none
  }
}

::v-deep .el-calendar-day {
  height:  auto;
 }
.date-content {
  height: 40px;
  text-align: center;
  line-height: 40px;
  font-size: 14px;
}
.date-content .rest {
  color: #fff;
  border-radius: 50%;
  background: rgb(250, 124, 77);
  width: 20px;
  height: 20px;
  line-height: 20px;
  display: inline-block;
  font-size: 12px;
  margin-left: 10px;
}
.date-content .text{
  width: 20px;
  height: 20px;
  line-height: 20px;
 display: inline-block;

}
.current{
   background: #409eff;
   color: #fff;
   border-radius: 50%;
 }
</style>
